package cn.iocoder.yudao.module.social.api.content;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.social.api.content.dto.ContentReqDTO;
import cn.iocoder.yudao.module.social.api.content.dto.ContentRespDTO;
import cn.iocoder.yudao.module.social.convert.content.ContentConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.content.ContentDO;
import cn.iocoder.yudao.module.social.service.content.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.social.enums.ErrorCodeConstants.CONTENT_NOT_EXISTS;

/**
 * 内容 API 实现类
 *
 * @author 芋道源码
 */
@RestController
@Validated
@Slf4j
public class ContentApiImpl implements ContentApi {

    @Resource
    private ContentService contentService;

    @Override
    public CommonResult<Long> createContent(ContentReqDTO reqDTO) {
        try {
            Long id = contentService.createContent(ContentConvert.INSTANCE.convertToCreateReqVO(reqDTO));
            return success(id);
        } catch (Exception ex) {
            log.error("[createContent][reqDTO({}) 创建内容异常]", reqDTO, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<Boolean> updateContent(ContentReqDTO reqDTO) {
        try {
            contentService.updateContent(ContentConvert.INSTANCE.convertToUpdateReqVO(reqDTO));
            return success(true);
        } catch (Exception ex) {
            log.error("[updateContent][reqDTO({}) 更新内容异常]", reqDTO, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<Boolean> deleteContent(Long id) {
        try {
            contentService.deleteContent(id);
            return success(true);
        } catch (Exception ex) {
            log.error("[deleteContent][id({}) 删除内容异常]", id, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<ContentRespDTO> getContent(Long id) {
        try {
            ContentDO content = contentService.getContent(id);
            if (content == null) {
                return CommonResult.error(CONTENT_NOT_EXISTS);
            }
            return success(ContentConvert.INSTANCE.convertApi(content));
        } catch (Exception ex) {
            log.error("[getContent][id({}) 查询内容异常]", id, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<ContentRespDTO>> getContentList(Collection<Long> ids) {
        try {
            List<ContentDO> contents = contentService.getContentList(ids);
            return success(ContentConvert.INSTANCE.convertApiList(contents));
        } catch (Exception ex) {
            log.error("[getContentList][ids({}) 批量查询内容异常]", ids, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<ContentRespDTO>> getContentListByContainer(Long containerId) {
        try {
            List<ContentDO> contents = contentService.getContentListByContainer(containerId);
            return success(ContentConvert.INSTANCE.convertApiList(contents));
        } catch (Exception ex) {
            log.error("[getContentListByContainer][containerId({}) 根据容器查询内容异常]", containerId, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<ContentRespDTO>> getContentListByAuthor(Long authorId) {
        try {
            List<ContentDO> contents = contentService.getContentListByAuthor(authorId);
            return success(ContentConvert.INSTANCE.convertApiList(contents));
        } catch (Exception ex) {
            log.error("[getContentListByAuthor][authorId({}) 根据作者查询内容异常]", authorId, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

}
